# ps 片 

# SOURCES   =$(wildcard ./src/*.txt)
SOURCES =  $(shell find src -type f -name '*.txt' -print  | xargs ls -tr)

# 监控脚本 chaiscipt的脚本
MON_SCRIPT = fchai/00_mon.chai

# display 动态显示时的延时 s
DIS_TIME ?= 1
# 
ifeq ($(OS),Windows_NT)  
# python3 = G:/soft/python312/python.exe
python3 = ./python312/python.exe
FCHAI= bin/fchai.exe
else 
python3 = python3
FCHAI= fchai
endif


# 绘图工具 python/gnuplot 
PLOTTOOL = python

out = output


default:help

run:plot std var dlt diff1 

# 计算信噪比 2000 ~ 2100 
# snr:clean $(SNR_FILE)
# 	@echo ----------- SNR -----------
# 	@cat $(out)/snr.snr 

toolchain:
ifeq ($(wildcard python312),)
	@echo "配置环境 ..."
	@git clone https://gitee.com/yuan_hp/python312.git && \
	cd python312 && cat python312.zip* | tar -xzv && rm python312.zip* -rf && \
	cp python312/* . -r && rm -rf python312 
endif

# 绘制能量图
energy:toolchain prepare
	@$(python3) script/energy.py $(SOURCES) > $(out)/ans.energy 
ifeq ($(PLOTTOOL), python)
	@$(python3) script/pyplot.py $(out)/ans.energy -x cm-1 -y y &
else
	@cp script/msplt.sh  $(out)/
	@cd $(out) && sed -i 's/x_dis_lab/cm^{-1}/' msplt.sh
	@cd $(out) && sed -i 's/y_dis_lab/能量图/' msplt.sh
	@cd $(out) && bash msplt.sh ans.energy
endif 

# 计算透射率
snr:toolchain prepare
	@$(python3) script/snr.py $(SOURCES) > $(out)/ans.snr 
	@cat $(out)/ans.snr 


# 计算透射率
tsl:toolchain prepare
	@$(python3) script/tsl.py $(SOURCES) > $(out)/ans.tsl 
ifeq ($(PLOTTOOL), python)
	@$(python3) script/pyplot.py $(out)/ans.tsl -x cm-1 -y tsl  &
else
	@cp script/msplt.sh  $(out)/
	@cd $(out) && sed -i 's/x_dis_lab/cm^{-1}/' msplt.sh
	@cd $(out) && sed -i 's/y_dis_lab/透射率/' msplt.sh
	@cd $(out) && bash msplt.sh ans.tsl
endif

# 计算吸光度 
xgd:toolchain prepare
	@$(python3) script/xgd.py $(SOURCES) > $(out)/ans.xgd 
ifeq ($(PLOTTOOL), python)
	@$(python3) script/pyplot.py $(out)/ans.xgd -x cm-1 -y xgd &
else
	@cp script/msplt.sh  $(out)/
	@cd $(out) && sed -i 's/x_dis_lab/cm^{-1}/' msplt.sh
	@cd $(out) && sed -i 's/y_dis_lab/吸光度/' msplt.sh
	@cd $(out) && bash msplt.sh ans.xgd
endif

# 计算12分钟稳定性
sigma:toolchain prepare
	@$(python3) script/sigma.py $(SOURCES) > $(out)/ans.sigma 
ifeq ($(PLOTTOOL), python)
	@$(python3) script/pyplot.py $(out)/ans.sigma -x cm-1 -y sigma  &
else
	@cp script/msplt.sh  $(out)/
	@cd $(out) && sed -i 's/x_dis_lab/cm^{-1}/' msplt.sh
	@cd $(out) && sed -i 's/y_dis_lab/吸光度标准差/' msplt.sh
	@cd $(out) && bash msplt.sh ans.sigma
endif 	

# 计算波数准确性
bszqx:toolchain prepare
	@$(python3) script/bszqx.py $(SOURCES) > $(out)/ans.bszqx 
ifeq ($(PLOTTOOL), python)
	@$(python3) script/pyplot.py $(out)/ans.bszqx -x cm-1 -y bszqx  &
else
	@cp script/msplt.sh  $(out)/
	@cd $(out) && sed -i 's/x_dis_lab/cm^{-1}/' msplt.sh
	@cd $(out) && sed -i 's/y_dis_lab/波数准确性/' msplt.sh
	@cd $(out) && cat ans.bszqx
	@cd $(out) && bash msplt.sh ans.bszqx
endif 

# 波数重复性
bscfx:toolchain prepare
	@$(python3) script/bscfx.py $(SOURCES) > $(out)/ans.bscfx 

	@cd $(out) && cat ans.bscfx
ifeq ($(PLOTTOOL), python)
	@$(python3) script/pyplot.py $(out)/ans.bscfx -x cm-1 -y bscfx &
else
	@cp script/msplt.sh  $(out)/
	@cd $(out) && sed -i 's/x_dis_lab/cm^{-1}/' msplt.sh
	@cd $(out) && sed -i 's/y_dis_lab/波数重复性(吸光度pp)/' msplt.sh
	@cd $(out) && bash msplt.sh ans.bscfx
endif 

# ------------------- 通用数据处理 -------------------
# 直接绘制原始数据  
# @echo $(SOURCES) > $(out)/src.txt 
# @xargs $(python3) script/plot.py < $(out)/src.txt  > $(out)/ans.plot 
plot:toolchain prepare
	@$(python3) script/plot.py $(SOURCES) > $(out)/ans.plot 
ifeq ($(PLOTTOOL), python)
	@$(python3) script/pyplot.py $(out)/ans.plot -t "plot" &
else
	@cp script/msplt.sh  $(out)/
	@cd $(out) && sed -i 's/x_dis_lab/x/' msplt.sh
	@cd $(out) && sed -i 's/y_dis_lab/y/' msplt.sh
	@cd $(out) && bash msplt.sh ans.plot
endif


display:toolchain prepare
	@echo $(shell find ./src -type f -name '*.txt' -print | xargs ls -tr)
	@$(python3) script/plot.py $(shell find src -type f -name '*.txt' -print  | xargs ls -tr) > $(out)/ans.display 
ifeq ($(PLOTTOOL), python)
	@$(python3) script/pydisplay.py $(out)/ans.display -d $(DIS_TIME)
else
	@echo "Not suport"
endif
# 计算原始数据标准差
std:toolchain prepare
	@$(python3) script/std.py $(SOURCES) > $(out)/ans.std 
ifeq ($(PLOTTOOL), python)
	@$(python3) script/pyplot.py $(out)/ans.std -t "std"  &
else
	@cp script/msplt.sh  $(out)/
	@cd $(out) && sed -i 's/x_dis_lab/x/' msplt.sh
	@cd $(out) && sed -i 's/y_dis_lab/y(标准差)/' msplt.sh
	@cd $(out) && bash msplt.sh ans.std
endif 

# 计算原始数据方差
var:toolchain prepare
	@$(python3) script/var.py $(SOURCES) > $(out)/ans.var 
ifeq ($(PLOTTOOL), python)
	@$(python3) script/pyplot.py $(out)/ans.var -t "var"  &
else
	@cp script/msplt.sh  $(out)/
	@cd $(out) && sed -i 's/x_dis_lab/x/' msplt.sh
	@cd $(out) && sed -i 's/y_dis_lab/y(方差)/' msplt.sh
	@cd $(out) && bash msplt.sh ans.var
endif 

# 计算原始数据方差
dlt:toolchain prepare
	@$(python3) script/dlt.py $(SOURCES) > $(out)/ans.dlt
ifeq ($(PLOTTOOL), python)
	@$(python3) script/pyplot.py $(out)/ans.dlt -t "dlt"  &
else 
	@cp script/msplt.sh  $(out)/
	@cd $(out) && sed -i 's/x_dis_lab/x/' msplt.sh
	@cd $(out) && sed -i 's/y_dis_lab/y(最大差值)/' msplt.sh
	@cd $(out) && bash msplt.sh ans.dlt
endif 

# 计算相对偏差
deviation:toolchain
	@$(python3) script/deviation.py $(SOURCES) > $(out)/ans.deviation
ifeq ($(PLOTTOOL), python)
	@$(python3) script/pyplot.py $(out)/ans.deviation -t "deviation"  &
else 
	@cp script/msplt.sh  $(out)/
	@cd $(out) && sed -i 's/x_dis_lab/x/' msplt.sh
	@cd $(out) && sed -i 's/y_dis_lab/y(最大差值)/' msplt.sh
	@cd $(out) && bash msplt.sh ans.deviation
endif 

maxdivmin:toolchain prepare
	@$(python3) script/maxdivmin.py $(SOURCES) > $(out)/ans.maxdivmin
ifeq ($(PLOTTOOL), python)
	@$(python3) script/pyplot.py $(out)/ans.maxdivmin  -x x -y maxdivmin -t maxdivmin &
else 
	@cp script/msplt.sh  $(out)/
	@cd $(out) && sed -i 's/x_dis_lab/x/' msplt.sh
	@cd $(out) && sed -i 's/y_dis_lab/y(maxdivmin)/' msplt.sh
	@cd $(out) && bash msplt.sh ans.maxdivmin
endif 

# 计算均值
ave:toolchain prepare
	@$(python3) script/ave.py $(SOURCES) > $(out)/ans.ave 
ifeq ($(PLOTTOOL), python)
	@$(python3) script/pyplot.py $(out)/ans.ave -t "ave" &
else
	@cp script/msplt.sh  $(out)/
	@cd $(out) && sed -i 's/x_dis_lab/x/' msplt.sh
	@cd $(out) && sed -i 's/y_dis_lab/y(均值)/' msplt.sh
	@cd $(out) && bash msplt.sh ans.ave
endif 


diff%:toolchain prepare
	@$(python3) script/$@.py $(SOURCES) > $(out)/ans.$@ 
ifeq ($(PLOTTOOL), python)
	@$(python3) script/pyplot.py $(out)/ans.$@ -t "$@" &
else
	@cp script/msplt.sh  $(out)/
	@cd $(out) && sed -i 's/x_dis_lab/x/' msplt.sh
	@cd $(out) && sed -i 's/y_dis_lab/y(均值)/' msplt.sh
	@cd $(out) && bash msplt.sh ans.$@
endif 

# 建立输出文件夹
prepare:
	@mkdir -p src/mon
	@cd src && bash ../script/csvToTxt.sh  
	@cd src && bash ../script/makeback.sh
	@mkdir -p $(out)
	@bash script/killgnuplot.sh 

# config.ini 文件换行根据系统自动适应
configlf:
ifeq ($(OS),Windows_NT)  
	@sed -i 's/\n$$/\r\n/g' config.ini
else
	@sed -i 's/\r$$//g' config.ini
endif


# ----------------------- 调用程序采集谱图 ----------------
#
grep:toolchain  configlf
	@mkdir -p src/mon 
	./script/GrepSpec
# ----------------------- 其他处理 -----------------------

txt:
	@rm src/*.back -rf 
	@sed -i 's/[ ]*$$//g' $(SOURCES)
	@sed -i 's/\x00//g' $(SOURCES)
	@sed -i 's/ \+/ /g' $(SOURCES)

index:
	@idx=1;for i in $(shell ls -tr src/*.txt) ; \
	do\
	  test ! -f "src/$$idx.txt" &&mv $$i src/$$idx.txt && echo "$$i --> src/$$idx.txt success"; \
	  ((idx=idx+1));\
	done 



# 重复抓取
grep-%:
	@mkdir -p src/mon
	@bash ./script/repeat $(shell echo $@ | cut -d - -f2) "./script/GrepSpec"

# 重复抓取并且采集状态量到state.txt
mon-%:
	@mkdir -p src/mon 
	@bash ./script/repeat $(shell echo $@ | cut -d - -f2) "./script/GrepSpec && $(FCHAI) $(MON_SCRIPT)"


help:
	@echo "------------------------------------------------------------------------------------------------"
	@echo "Note:中红外设备测试数据处理使用example"
	@echo "  make tsl         计算透射率并绘制结果"
	@echo "  make xgd         计算吸光度并绘制结果"
	@echo "  make sigma       计算数据稳定性(例如12min 吸光度)并绘制结果"
	@echo "  make snr         计算信噪比"
	@echo "  make energy      绘制能量图"
	@echo "  make bszqx       计算波数准确性"
	@echo "  make bscfx       计算波数重复性"
	@echo "Note:通用数据处理"
	@echo "  make txt         校正数据源格式"
	@echo "  make index       原始数据按建立时间修正文件名成为数字"
	@echo "  make plot        原始数据绘制显示"
	@echo "  make display     原始数据动画绘制显示,可以看到变化过程,间隔时间为$(DIS_TIME)s"
	@echo "  make std         标准差计算并显示"
	@echo "  make var         方差计算并显示"
	@echo "  make dlt         计算每个横轴上y值的最大偏差"
	@echo "  make diff1       计算并绘制一阶导数"
	@echo "  make diff2       计算并绘制二阶导数"
	@echo "  make grep        光谱数据抓取"
	@echo "  make grep-n      连续n次光谱数据抓取,例如 make grep-6"
	@echo "  make mon-n       连续n次光谱数据抓取,并且采集温度等状态到 src/mon/mon.log,例如 make grep-6"
	@echo "  make backup      将src文件夹移到"
	@echo "  make maxdivmin   每个x对应数据下最大值除以最小值并绘图"
	@echo "  make deviation   计算相对偏差"
	@echo "  make clean_src   删除src下的所有文件"
	@echo "  make toolchain   拉取数据分析的工具链"
	@echo "Example:"
	@echo "  make mon-6 && make plot  采集6张能量图并监控设备参数，完成后绘图"
	@echo "  make run         一次通用绘图"
	@echo "------------------------------------------------------------------------------------------------"

sub-%:
	./mana.sh w $(shell echo $@ | cut -d - -f2)  $(shell echo $@ | cut -d - -f3)

backup:
	@bash script/backup.sh 
	
clean:
	@bash script/killgnuplot.sh 
	@rm -rf $(out) state.txt src/*
	@sed -i 's/id=.*/id=1/g' config.ini 


clean_src:
	@bash script/killgnuplot.sh 
	@rm -rf ./src/* 


include sub.mk 

